<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="培训流水号" prop="trainId">
        <el-input
          v-model="queryParams.trainId"
          placeholder="请输入培训流水号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['train:train:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['train:train:remove']"
        >取消
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['train:train:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border fit v-loading="loading" :data="trainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="培训流水号" align="center" prop="trainId"/>
      <el-table-column label="申请人姓名" align="center" prop="userName"/>
      <el-table-column label="申请人部门" align="center" prop="userDept"/>
      <el-table-column label="课程名称" align="center" prop="trainName"/>
      <el-table-column label="培训类型" align="center" prop="trainType"/>
      <el-table-column label="培训学时" align="center" prop="trainHours"/>
      <el-table-column label="培训时间" align="center" prop="trainTime" width="190">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.trainTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="trainIdea">
        <template slot-scope="{row}">
          <el-tag
            type="info"
            v-show="row.trainIdea===0"
          >
            未审批
          </el-tag>
          <el-tag
            type="success"
            v-show="row.trainIdea===1"
          > 同意
          </el-tag>
          <el-tag
            type="danger"
            v-show="row.trainIdea===2"
          > 不同意
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-zoom-in"
            @click="handleView(scope.row)"
            v-hasPermi="['train:train:query']"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['train:train:remove']"
          >取消
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加培训对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">

        <el-row>
          <el-col :span="19" :offset="1">
            <el-form-item label="课程名称" prop="trainName">
              <el-input v-model="form.trainName" placeholder="请输入课程名称"/>
            </el-form-item>

            <el-form-item label="培训类型:" prop="trainType">
              <el-select
                class="ele-block"
                v-model="form.trainType"
                placeholder="请选择培训类型"
              >
                <el-option
                  v-for="item in trainTypeList"
                  :key="item.trainTypeId"
                  :label="item.trainTypeName"
                  :value="item.trainTypeName"
                >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="培训学时/小时" prop="trainHours">
              <el-input-number v-model="form.trainHours" :precision="1" :step="0.5" :min="0" size="small"
              ></el-input-number>
            </el-form-item>

            <el-form-item label="培训时间" prop="trainTime">
              <el-date-picker
                v-model="form.trainTime"
                align="right"
                type="date"
                placeholder="选择日期"
                :picker-options="pickerOptions"
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item label="主办单位" prop="trainHostDept">
              <el-input v-model="form.trainHostDept" placeholder="请输入主办单位"/>
            </el-form-item>

            <el-form-item label="培训地点" prop="trainPlace">
              <el-input v-model="form.trainPlace" placeholder="请输入培训地点"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="19" :offset="1">
            <el-form-item label="课程内容" prop="trainContent">
              <el-input v-model="form.trainContent" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="备注" prop="trainRemarks">
              <el-input v-model="form.trainRemarks" type="textarea" placeholder="请输入备注"/>
            </el-form-item>
            <el-form-item label="相关证书">
              <file-upload :limit="20" :fileSize="50" :fileType="['png', 'jpg', 'jpeg','doc','pdf','xls','ppt']" v-model="form.trainFile"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTrain, getTrain, delTrain, addTrain, updateTrain, queryTrainType } from '@/api/train/train'
import { querySealType } from '@/api/seal/seal'
import { selectMinTrainTime } from '@/api/train/trainType'

export default {
  name: 'Train',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 培训表格数据
      trainList: [],
      // 培训类型数据
      trainTypeList: [],
      //已选择培训数据
      trainTypeValue: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        trainId: null
      },
      // 表单参数
      form: {},

      // 表单校验
      rules: {
        trainHostDept: [
          { required: true, message: '员工类型不能为空', trigger: 'blur' }
        ],
        trainPlace: [
          { required: true, message: '员工类型不能为空', trigger: 'blur' }
        ],
        trainName: [
          { required: true, message: '课程名称不能为空', trigger: 'blur' }
        ],
        trainType: [{
          required: true, message: '用印类型不能为空', trigger: 'change'
        }],
        trainHours: [
          { required: true, message: '培训学时不能为空', trigger: 'blur' }
        ],
        trainContent: [
          { required: true, message: '课程内容不能为空', trigger: 'blur' }
        ],
        trainTime: [
          { required: true, message: '培训日期不能为空', trigger: 'change' }]
      },
      pickerOptions: {
        shortcuts: [
          {
            text: '明天',
            onClick(picker) {
              const date = new Date()
              date.setTime(date.getTime() + 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          },
          {
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date())
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date()
              date.setTime(date.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', date)
            }
          }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询培训列表 */
    getList() {
      this.loading = true
      listTrain(this.queryParams).then(response => {
        this.trainList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        trainId: null,
        userId: null,
        userName: null,
        userDept: null,
        userLeibie: null,
        userLeixing: null,
        trainName: null,
        trainType: null,
        trainHours: null,
        trainTime: null,
        trainContent: null,
        trainRemarks: null,
        trainFile: null,
        trainIdea: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.trainId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    async handleAdd() {
      this.reset()
      let minTime
      await selectMinTrainTime().then(response => {
        minTime = response.msg
        this.$alert('申请时培训最低时间：' + minTime + '/小时')
      })
      queryTrainType().then(response => {
        this.trainTypeList = response.data.trainType
      })
      this.open = true
      this.title = '新建培训记录'
    },

    /** 查看按钮跳转操作 */
    handleView(row) {
      this.$router.push({ path: '/ViewTrainExcel', query: { trainId: row.trainId } })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          addTrain(this.form).then(response => {
            this.$modal.msgSuccess('新增成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const trainIds = row.trainId || this.ids
      this.$modal.confirm('是否确认删除培训编号为"' + trainIds + '"的数据项？').then(function() {
        return delTrain(trainIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('train/export', {
        ...this.queryParams
      }, `train_${new Date().getTime()}.xlsx`)
    }

  }
}
</script>
